package com.avcdata.aoscustommade.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;

import java.util.List;

/**
 * Created by c.c on 2017/3/1.
 * 选择万能选择适配器
 */
public class BaseHolderAdapter extends BaseAdapter {
    protected List<String> data;

    private Context mContext;
    private int selectPosition;

    public BaseHolderAdapter(Context mContext, List<String> data) {
        super();
        this.mContext = mContext;
        this.data = data;
        this.selectPosition = -1;
    }


    public void setData(List<String> data) {
        this.data = data;
    }

    public void addData(List<String> data) {
        if (this.data == null) {
            this.data = data;
        } else {
            this.data.addAll(data);
        }
        notifyDataSetChanged();
    }

    public void addData(String bean) {
        if (this.data != null) {
            this.data.add(bean);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        if (this.data == null) {
            return;
        } else {
            this.data.clear();
        }
        notifyDataSetChanged();
    }

    private int count = -1;

    public void setCount(int i) {
        count = i;
    }


    public int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public String getSelectString() {
        if (selectPosition < 0 ||this.data.size() <= selectPosition) {
            return "";
        }
        return data.get(selectPosition).toString().trim();
    }




    @Override
    public int getCount() {
        if (data == null) {
            return 0;
        }
        int getCount = data.size();
        return count == -1 ? getCount : Math.min(getCount, count);
    }

    @Override
    public Object getItem(int position) {

        return data == null ? new Object() : data.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.single_textview, null);
            vHolder = new ViewHolder();
            vHolder.single_textview = (TextView) convertView.findViewById(R.id.single_textview);
            convertView.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) convertView.getTag();
        }

        vHolder.single_textview.setText(data.get(position));

        return convertView;

    }




    private class ViewHolder {
        TextView single_textview;


    }


}
